﻿namespace TPXSDK.NET.Share;

using System;

/// <summary>
/// 时间工具类
/// </summary>
public class TimeUtils
{
    /// <summary>
    /// UTC时间戳转换本地时间
    /// </summary>
    public static DateTime GetDateTime(double timestamp)
    {
        var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        return origin.AddSeconds(timestamp);
    }

    /// <summary>
    /// 本地时间转换UTC时间戳
    /// </summary>
    public static double GetUTC(DateTime date)
    {
        var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        var diff = date - origin;
        return Math.Floor(diff.TotalSeconds);
    }

    /// <summary>
    /// 获得当前UTC时间戳
    /// </summary>
    public static double CurrentUTC
    {
        get
        {
            var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var diff = DateTime.Now - origin;
            return Math.Floor(diff.TotalSeconds);
        }
    }
}

